package com.igoal.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="goal")
@Searchable
public class Goal extends BaseObject implements Serializable, Audit, ItemOwner {
	private static final long serialVersionUID = -9132983189500613773L;
	
	private Long id;
	private String title;
	private Integer userCount;
	private Integer doneCount;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;
	
	private MustDoGoal mustDoGoal;
	
	private Set<Goal> goals = null;
	private Set<Category> categories = null;
	
	private Set<UserGoal> userGoals = new HashSet<UserGoal>(0);
	private Set<GoalCategory> userCategories = new HashSet<GoalCategory>(0);
	
	public Goal(){}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}
	
	@OneToMany(fetch=FetchType.LAZY, mappedBy="pk.goal")
	public Set<UserGoal> getUserGoals() {
		return userGoals;
	}
		
	public void setUserGoals(Set<UserGoal> goals) {
		this.userGoals = goals;
	}
		
	public void setUserCategories(Set<GoalCategory> categories) {
		this.userCategories = categories;
	}
	
	@OneToMany(fetch=FetchType.LAZY, mappedBy="pk.goal")
	public Set<GoalCategory> getUserCategories() {
		return userCategories;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
            return true;
        }
        if (!(o instanceof Goal)) {
            return false;
        }

        final Goal goal = (Goal) o;

        return !(title != null ? !title.equals(goal.getTitle()) : goal.getTitle() != null);
	}

	@Override
	public int hashCode() {
		return this.title.hashCode();
	}

	@Override
	public String toString() {
		return this.title;
	}

	@Column(nullable=false, name="goal_title")
	@SearchableProperty
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(nullable=false, name="num_users")
	public Integer getUserCount() {
		return userCount;
	}

	public void setUserCount(Integer userCount) {
		this.userCount = userCount;
	}
	
	@Column(nullable=false, name="num_done")
	public Integer getDoneCount() {
		return doneCount;
	}

	public void setDoneCount(Integer doneCount) {
		this.doneCount = doneCount;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Transient
	public Set<Goal> getGoals() {
		if(goals == null){
			goals = new HashSet<Goal>(0);
			for(UserGoal userGoal : userGoals){
				goals.add(userGoal.getGoal());
			}
		}
		return goals;
	}
	
	@Transient
	public Set<Category> getCategories() {
		if(categories == null){
			categories = new HashSet<Category>(0);
			for(GoalCategory userCategory : userCategories){
				categories.add(userCategory.getCategory());
			}
		}
		
		return categories;
	}
	
	@Transient
	public User getOwner() {
		// for now, their're any user own the goal, they just implement the goal
		return null;
	}
	
	@OneToOne(fetch = FetchType.LAZY, mappedBy = "goal")
	@Cascade({ CascadeType.SAVE_UPDATE })
	public MustDoGoal getMustDoGoal() {
		return mustDoGoal;
	}

	public void setMustDoGoal(MustDoGoal mustDoGoal) {
		this.mustDoGoal = mustDoGoal;
	}
	
	
}
